package com.ruoyi.platform.system.user.service;

import com.ruoyi.base.service.BaseService;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.platform.system.user.dao.UserDAO;
import com.ruoyi.platform.system.user.manager.UserManager;
import com.ruoyi.platform.system.user.manager.role.UserRoleManager;
import com.ruoyi.platform.system.user.pojo.UserDO;
import com.ruoyi.platform.system.user.pojo.UserPlatQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 注册
 *
 * @author lcf
 */
@Service
public class RegisterService extends BaseService {
    @Autowired
    private UserDAO sysUserDao;
    @Autowired
    private UserManager userManager;
    @Autowired
    private UserRoleManager userRoleManager;

    /**
     * 后台新增用户
     */
    @Transactional(rollbackFor = Exception.class)
    public void register(UserPlatQuery query, List<Long> roleCodeList) {
        //检查账号合法性
        InputCheckUtils.checkAccount(query.getAccount());
        InputCheckUtils.checkChineseWord(query.getAccount());
        //检查账号是否存在
        userManager.checkAccountExist(null, query.getAccount());
        //检查手机号是否存在
        userManager.checkPhoneExist(null, query.getPhone());

        UserDO user = query.convert();
        sysUserDao.insertUserSelective(user);

        userRoleManager.editUserRole(user.getUserId(), roleCodeList);
    }

}
